// 加载环境变量
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// 加载对应环境变量的配置文件
dotenv.config({
    path: path.join(__dirname, `./env/.${process.env.NODE_ENV}.env`)
});

export const config = {
    // 服务器配置
    port: process.env.PORT || 3000,
    host: process.env.HOST || 'localhost',
    
    // 数据库配置
    database: {
        host: process.env.DB_HOST || 'localhost',
        port: process.env.DB_PORT || 3306,
        name: process.env.DB_NAME || 'mihupig_db',
        user: process.env.DB_USER || 'root',
        password: process.env.DB_PASSWORD || ''
    },
    
    // Redis配置
    redis: {
        host: process.env.REDIS_HOST || 'localhost',
        port: process.env.REDIS_PORT || 6379,
        password: process.env.REDIS_PASSWORD || null
    },
    
    // JWT配置
    jwt: {
        secret: process.env.JWT_SECRET || 'default-secret-key',
        expiresIn: process.env.JWT_EXPIRES_IN || '24h'
    },
    
    // CORS配置
    cors: {
        origin: process.env.CORS_ORIGIN === '*' ? '*' : (process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : ['http://localhost:3000']),
        credentials: process.env.CORS_CREDENTIALS === 'true'
    },
    
    // 日志配置
    log: {
        level: process.env.LOG_LEVEL || 'info',
        console: process.env.LOG_CONSOLE !== 'false',
        file: process.env.LOG_FILE === 'true'
    },
    
    // API配置
    api: {
        baseUrl: process.env.API_BASE_URL || `http://localhost:${process.env.PORT || 3000}/api`,
        timeout: parseInt(process.env.API_TIMEOUT) || 5000
    },
    
    // 环境标识
    env: process.env.NODE_ENV || 'development'
}